<script lang="ts" setup>
import { FinAccountApi } from '@/apis'

const props = withDefaults(defineProps<Props>(), {
  placeholder: '选择财务账户',
  keyword: undefined,
  excludeOnline: false,
})

interface Props {
  placeholder?: string
  options?: any[]
  excludeOnline?: boolean
}

const loading = ref(false)

const modal = defineModel<string>()
const dataList = ref<any[]>([])
const _options = computed(() => {
  return dataList.value.map(item => ({ label: item.name, value: item.id }))
})
const getOptionList = () => {
  loading.value = true
  FinAccountApi.getList({})
    .then((res) => {
      if (props.excludeOnline) {
        dataList.value = res.filter(item => item.id !== 'FUIOU')
      } else {
        dataList.value = res
      }
    })
    .finally(() => loading.value = false)
}

onMounted(async () => {
  if (props.options) {
    dataList.value = props.options
  } else {
    getOptionList()
  }
})
</script>

<template>
  <a-select v-bind="$attrs" v-model="modal" :placeholder="placeholder" :loading="loading" :options="_options" :filter-option="true" :allow-search="true" allow-clear />
</template>

<style scoped>
</style>
